package controller.v2;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import listener.WebContextListener;
import model.MemberVO;
import service.v2.MemberService;
import util.DateParseUtil;
import util.OssUtil;
import util.PackageUtil;
import util.PropertieUtil;

@RequestMapping("/v2/info")
@Controller
public class InfoController { 
	@Autowired
	private MemberService memberService;
	/**
	 *获取个人信息
	 * @param token  令牌
	 * @param member_id  用户ID
	 */
	@ResponseBody
	@RequestMapping(value = "/loginInfo", method = RequestMethod.GET)
	public Object loginInfo(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) {
		MemberVO vo = memberService.getLoginMemberById(member_id);
		return PackageUtil.packageParams(200,"success",vo,""); 
	}
	
	/**
	 *获取邀请人信息
	 * @param token  令牌
	 * @param member_id  用户ID
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/inviteInfo", method = RequestMethod.GET)
	public Object inviteInfo(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) throws Exception {
		memberVO = memberService.getInvitedMemberInfo(member_id);
		if(memberVO == null){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","","没有邀请人"); 
		}
		String invited_by_update = memberService.getInvitedByUpdate(member_id);
		memberVO.setInvited_by_update(invited_by_update);
		return PackageUtil.packageParams(200,"success",memberVO,""); 
	}
	
	/**
	 *修改提交
	 * @param token  令牌
	 * @param member_id  用户ID
	 * logo 头像
	 * nick_name 昵称
	 * gender 性别  M男 F女
	 * birthday 生日
	 */
	@ResponseBody
	@RequestMapping(value = "/infoUpdateSub", method = RequestMethod.POST)
	public Object infoUpdateSub(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) {
		//参数
		if((memberVO.getLogo()==null || "".equals(memberVO.getLogo()))
				&&(memberVO.getNick_name()==null || "".equals(memberVO.getNick_name()))
				&&(memberVO.getGender()==null || "".equals(memberVO.getGender()))
			    &&(memberVO.getBirthday()==null || "".equals(memberVO.getBirthday()))	){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(memberVO.getNick_name()!=null && memberVO.getNick_name().length() > 20){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(memberVO.getGender() != null && !"M".equals(memberVO.getGender()) && !"F".equals(memberVO.getGender())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(memberVO.getBirthday() != null && !DateParseUtil.isValidDate(memberVO.getBirthday())){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		memberVO.setId(member_id);
		memberService.updateMemberInfo(memberVO);
		return PackageUtil.packageParams(200,"success","",""); 
	}
	
	/**
	 * 验证并获取邀请人信息
	 * @param token  令牌
	 * @param member_id  用户ID
	 * tel邀请人电话/邀请码/用户id编号
	 * country_id 国家ID
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/checkInvited", method = RequestMethod.GET)
	public Object checkInvited(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) throws Exception {
		if(memberVO.getTel()==null || "".equals(memberVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","","");
		}
		//参数格式
		if(memberVO.getTel().length() != 11 && memberVO.getTel().length() != 8 && memberVO.getTel().length() != 6){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		//验证存在
		MemberVO im = memberService.getMemberByTelNoInviteno(memberVO);
		if(im==null){
			return PackageUtil.packageParams(13002, "用户不存在", "", "");
		}
		MemberVO vo = memberService.getAllInfoById(member_id);
		//验证是同一个人
		if(vo.getInvited_by() != null && vo.getInvited_by().equals(im.getId())){
			return PackageUtil.packageParams(13008, "新推荐人不可与原推荐人相同", "", "");
		}
		//邀请人不能是本人
		if(member_id.equals(im.getId())){
			return PackageUtil.packageParams(13003, "邀请人不能是您本人", "", "");
		}
		//检查邀请人是否可以修改
		if(vo.getInvited_by() != null && !"".equals(vo.getInvited_by()) ){
			String invited_by_update = memberService.getInvitedByUpdate(member_id);
			memberVO.setInvited_by_update(invited_by_update);
		}else{
			memberVO.setInvited_by_update("1");
		}
		if("pe".equals(WebContextListener.env_code)){
			//如果是666的只能绑定666的
			if(vo.getTel().startsWith("666") && !im.getTel().startsWith("666")){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","","不可绑定非测试帐号");
			}
			if(!vo.getTel().startsWith("666") && im.getTel().startsWith("666")){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","","不可绑定非测试帐号");
			}
		}
		//邀请人属于自己团队
		int num = memberService.checkMemberInChildren(im.getId(),member_id);
		if(num>0){
			return PackageUtil.packageParams(13006, "该用户是您的团队成员，无法成为您的推荐人", "", "");
		}
		return PackageUtil.packageParams(200,"success",im,""); 
	}
	
	/**
	 * 修改邀请人信息
	 * @param token  令牌
	 * @param member_id  用户ID
	 * tel邀请人电话 
	 * country_id 国家ID
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value = "/updateInvited", method = RequestMethod.POST)
	public Object updateInvited(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) throws Exception {
		//兼容旧版本
		if(memberVO.getCountry_id()==null || "".equals(memberVO.getCountry_id())){
			memberVO.setCountry_id("1");
		}
		if(memberVO.getTel()==null || "".equals(memberVO.getTel())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		//参数格式
		if(memberVO.getTel().length() != 11){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		MemberVO vo = memberService.getAllInfoById(member_id);
		//验证是同一个人
		MemberVO im = memberService.getMemberByTelAndCountry(memberVO);
		if(im==null){
			return PackageUtil.packageParams(13002, "用户不存在", "", "");
		}
		//检查邀请人是否可以修改
		if(vo.getInvited_by() != null && !"".equals(vo.getInvited_by())){
			String invited_by_update = memberService.getInvitedByUpdate(member_id);
			if("0".equals(invited_by_update)){
				return PackageUtil.packageParams(13004, "推荐人已设置，不能修改", "", "");
			}
			memberVO.setInvited_by_record("Y");
		}
		if(vo.getInvited_by() != null && vo.getInvited_by().equals(im.getId())){
			return PackageUtil.packageParams(13008, "新推荐人不可与原推荐人相同", "", "");
		}
		if("pe".equals(WebContextListener.env_code)){
			//如果是666的只能绑定666的
			if(vo.getTel().startsWith("666") && !im.getTel().startsWith("666")){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","","不可绑定非测试帐号");
			}
			if(!vo.getTel().startsWith("666") && im.getTel().startsWith("666")){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","","不可绑定非测试帐号");
			}
		}
		//邀请人不能是本人
		if(member_id.equals(im.getId())){
			return PackageUtil.packageParams(13003, "邀请人不能是您本人", "", "");
		}
		//邀请人属于自己团队
		int num = memberService.checkMemberInChildren(im.getId(),member_id);
		if(num>0){
			return PackageUtil.packageParams(13006, "该用户是您的团队成员，无法成为您的推荐人", "", "");
		}
		memberVO.setId(member_id);
		memberVO.setInvited_by(im.getId());
		memberService.updateInvitedBy(memberVO);
		return PackageUtil.packageParams(200,"success","",""); 
	}
	/**
	 * 设置个性签名
	 * @param token  令牌
	 * @param member_id  用户ID
	 * @param personal_sign  个性签名
	 * tel邀请人电话 
	 */
	@ResponseBody
	@RequestMapping(value = "/updatepersonalsign", method = RequestMethod.POST)
	public Object updatepersonalsign(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, HttpServletRequest request,
			HttpServletResponse response, MemberVO memberVO) {
		if(memberVO.getPersonal_sign()==null){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		if(memberVO.getPersonal_sign().length() >30 ){
			return PackageUtil.packageParams(13007,"内容长度超出了限制","",""); 
		}
		memberVO.setId(member_id);
		memberService.updatePersonalSign(memberVO);
		
		return PackageUtil.packageParams(200,"success","",""); 
	}
	
	/**
	 * 微信端上传头像
	 */
	@ResponseBody
	@RequestMapping(value = "/upImg", method = RequestMethod.POST)
	public Object upImg(@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, 
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
		MemberVO vo= memberService.getAllInfoById(member_id);
		String fileName=OssUtil.uploadFile(file, "Upload/member/"+vo.getId()+"/");
		if(fileName != null){
//			if(vo.getLogo() != null && !"".equals(vo.getLogo())){  默认头像不能删除
//				OssUtil.deleteFile(vo.getLogo());
//			}
			MemberVO memberVO = new MemberVO();
			memberVO.setId(member_id);
			memberVO.setLogo(fileName);
			memberService.updateMemberInfo(memberVO);
		}
		return PackageUtil.packageParams(200,"success",fileName,""); 
	}
}
